//
//  MBCommodityEffectiveCell.swift
//  StarTimesON
//
//  Created by Toj on 8/22/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

protocol MBCommodityEffectiveCellDelegate: NSObjectProtocol {
    func commodityEffectiveCellDidSelected(_ view: MBCommodityEffectiveCell)
}

private let discountLabelHeight: CGFloat = 14
// MARK: - 懒加载(属性), 变量, 初始化init
class MBCommodityEffectiveCell: UICollectionViewCell {
    
    weak var delegate: MBCommodityEffectiveCellDelegate?
    // 固定有效期
    var model: MemberCommodityDto? { didSet {
        guard let commodity = model else { return }
        
        backImageView.isSelected = commodity.isSelected
        dateView.isSelected = commodity.isSelected
        
        let date = DateFormat.ymdhmsPointFormatter.string(from: Date(millisecondsSince1970: commodity.end_time))
        dateView.setTitle(date, for: .normal)
        
        // 商品吊牌价
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail
        paragraphStyle.alignment = .center
        let symbol = commodity.currency_symbol
        let price = commodity.price
        var font = UIFont.bold(with: 10.f_scale)
        let symbolAttributed = symbol.to_attributedString(font: font, color: priceLabel.textColor, paragraph: paragraphStyle)
        
        font = UIFont.boldSystemFont(ofSize: 21.f_scale)
        let priceAttributed = price.st_moneyFormat.st_cleanZero.to_attributedString(font: font, color: priceLabel.textColor, paragraph: paragraphStyle)
        symbolAttributed.append(priceAttributed)
        priceLabel.attributedText = symbolAttributed
        
        // 商品折扣价
        let list_price = commodity.list_price
        let list_priceString = symbol + list_price.st_moneyFormat.st_cleanZero
        let invalidPriceAttributed = list_priceString.to_attributedString(font: invalidPriceLabel.font, color: invalidPriceLabel.textColor, paragraph: paragraphStyle, lineStyle: .strikethrough)
        invalidPriceLabel.attributedText = invalidPriceAttributed
        invalidPriceLabel.isHidden = list_price <= price
        setupMakeRelayoutSubviews()
        if(!commodity.isShowed) {
            commodity.isShowed = true
            let ext = [DSKExtension.channel_info.rawValue: commodity.channelInfo]
            let event = DSEvent(category: st_className, action: "ProdShow", label: commodity.name, value: "1")
            DataStatistics.sendEvent(event: event, msg: ext)
        }
    } }
    
    @objc func didTapEffective() {
        delegate?.commodityEffectiveCellDidSelected(self)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        
        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer.init(target:self, action:#selector(didTapEffective))
        addGestureRecognizer(tapGesture)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 选择边框View
    private lazy var backImageView: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.setBackgroundImage("commodity_border_normal".to_image, for: .normal)
        view.setBackgroundImage("commodity_border_sel".to_image, for: .selected)
        return view
    }()
    
    // 时间
    private lazy var dateView: UIButton = {
        let view = UIButton()
        view.titleLabel?.textAlignment = .center
        view.isUserInteractionEnabled = false
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16.f_scale)
        view.setTitleColor("#775E33".to_color, for: .normal)
        view.setBackgroundImage("commodity_mb_bundled_bg_normal".to_image, for: .normal)
        view.setBackgroundImage("commodity_mb_bundled_bg_sel".to_image, for: .selected)
        return view
    }()
    
    // 现价格
    private lazy var priceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#554422".to_color
        return view
    }()
    
    // 原价
    private lazy var invalidPriceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#AAAAAA".to_color
        view.font = UIFont.systemFont(ofSize: 14.f_scale)
        return view
    }()
}

// MARK: - 初始化
extension MBCommodityEffectiveCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(dateView)
        contentView.addSubview(backImageView)
        contentView.addSubview(priceLabel)
        contentView.addSubview(invalidPriceLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        let width = STScreenWidth - 16 * 2
        let scale = width / 328
        backImageView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        dateView.snp.makeConstraints { make in
            make.top.bottom.left.equalTo(contentView)
            make.width.equalTo(216*scale)
        }
    }
    
    private func setupMakeRelayoutSubviews() {
        if !invalidPriceLabel.isHidden {
            invalidPriceLabel.snp.remakeConstraints { make in
                make.left.equalTo(dateView.snp.right)
                make.right.equalTo(contentView)
                make.bottom.equalTo(contentView.snp.bottom).offset(-12)
            }
            
            priceLabel.snp.remakeConstraints { make in
                make.width.left.equalTo(invalidPriceLabel)
                make.bottom.equalTo(invalidPriceLabel.snp.top)
            }
            return
        }
        
        priceLabel.snp.remakeConstraints { make in
            make.left.equalTo(dateView.snp.right)
            make.centerY.right.equalTo(contentView)
        }
    }
}
